package com.miyouke.chat.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miyouke.chat.dao.AppControlMapper;
import com.miyouke.chat.dto.AppControlDTO;
import com.miyouke.chat.entity.AppControl;
import com.miyouke.chat.entity.enums.AppStatusEnum;
import com.miyouke.chat.exception.GlobalException;
import com.miyouke.chat.listener.SShSessionListener;
import com.miyouke.chat.service.AppControlService;
import com.miyouke.chat.util.BeanUtil;
import com.miyouke.chat.util.SshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AppControlServiceImpl extends ServiceImpl<AppControlMapper, AppControl> implements AppControlService {

    @Autowired
    private SShSessionListener sshSessionListener;


    @Override
    public String startApp(List<Integer> ids) {

        String result = "";
        if(CollectionUtil.isEmpty(ids)){
            throw new GlobalException("未查询到内容！");
        }
        List<AppControl> appControls = listByIds(ids);
        for (int i = 0; i < appControls.size(); i++) {
            result = execCmd(result, appControls.get(i), AppStatusEnum.OPEN);
        }
        return result;
    }

    @Override
    public String stopApp(List<Integer> ids) {

        String result = "";
        if(CollectionUtil.isEmpty(ids)){
            throw new GlobalException("未查询到内容！");
        }
        List<AppControl> appControls = listByIds(ids);

        for (int i = 0; i < appControls.size(); i++) {
            result = execCmd(result, appControls.get(i), AppStatusEnum.CLOSE);
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public List<AppControl> applist() {

        List<AppControl> list = list();
        for (int i = 0; i < list.size(); i++) {
            AppControl appControl = list.get(i);
            AppControlDTO transform = BeanUtil.transform(AppControlDTO.class, appControl);
            appControl.setStatus("关");
            SshUtil sshUtil = sshSessionListener.getSessionMap(transform);
            if(sshUtil == null){
                appControl.setStatus("关");
            }else {
                StringBuilder command = new StringBuilder();
                command.append("netstat -tlnp | grep :").append(appControl.getAppPort());
                try {
                    String commandOutput = sshUtil.exec(command.toString());
                    System.out.println(commandOutput);
                    if (commandOutput.contains("LISTEN")) {
                        appControl.setStatus("开");
                    } else {
                        appControl.setStatus("关");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    private String execCmd(String result,  AppControl appControl, AppStatusEnum appStatusEnum) {

        AppControlDTO transform = BeanUtil.transform(AppControlDTO.class, appControl);
        SshUtil sshUtil = sshSessionListener.getSessionMap(transform);
        if(sshUtil == null){
            result += "连接失败：" + transform.getHost() + "\n";
        }
        String cmd = "sh ";
        if(appStatusEnum.equals(AppStatusEnum.OPEN)){
            cmd = cmd + appControl.getStartFile();
        }else {
            cmd = cmd + appControl.getStopFile();
        }
        try {
            System.out.println( " 执行cmd： " + cmd);
            String exec = sshUtil.exec(cmd);
            System.out.println( " 执行结果展示： " + exec);
        }catch ( Exception e ){
            throw new GlobalException("执行失败！");
        }
        return result;
    }


}
